package com.laiketui.common.service.dubbo.ocr;


import com.alibaba.fastjson.JSONObject;
import com.aliyun.dytnsapi20200217.Client;
import com.aliyun.dytnsapi20200217.models.CertNoTwoElementVerificationResponse;
import com.aliyun.dytnsapi20200217.models.CertNoTwoElementVerificationResponseBody;
import com.aliyun.ocr_api20210707.models.VerifyBusinessLicenseRequest;
import com.aliyun.ocr_api20210707.models.VerifyBusinessLicenseResponse;
import com.aliyun.teaopenapi.models.Config;
import com.laiketui.common.api.ocr.VerifyService;
import com.laiketui.common.mapper.UserBusinessInfoMapper;
import com.laiketui.common.utils.tool.str.StringUtils;
import com.laiketui.core.exception.LaiKeAPIException;
import com.laiketui.core.lktconst.GloabConst;
import com.laiketui.domain.lktconst.ErrorCode;
import com.laiketui.domain.ocr.VerifyBusinessData;
import com.laiketui.domain.user.UserBusinessInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VerifyServiceImpl implements VerifyService {
    final String authCode = "2f2e8tdaNg";

    @Autowired
    private UserBusinessInfoMapper userBusinessInfoMapper;

    @Override
    public String verifyIdCard(String name, String idNumber) throws Exception {
        Client client = this.createVerifyIdCardClient();
        String isConsistent = "";
        com.aliyun.dytnsapi20200217.models.CertNoTwoElementVerificationRequest certNoTwoElementVerificationRequest = new com.aliyun.dytnsapi20200217.models.CertNoTwoElementVerificationRequest()
                .setAuthCode(authCode)
                .setCertName(name)
                .setCertNo(idNumber);
        //身份证二要素校验
        CertNoTwoElementVerificationResponse verificationResponse = client.certNoTwoElementVerification(certNoTwoElementVerificationRequest);
        CertNoTwoElementVerificationResponseBody body = verificationResponse.getBody();
        CertNoTwoElementVerificationResponseBody.CertNoTwoElementVerificationResponseBodyData bodyData = body.getData();
        isConsistent = bodyData.isConsistent;
        return isConsistent;
    }

    @Override
    public VerifyBusinessData verifyBusinessLicense(String creditCode, String companyName, String legalPerson) throws Exception {
        if(StringUtils.isEmpty(creditCode) || StringUtils.isEmpty(companyName) || StringUtils.isEmpty(legalPerson)){
            throw new LaiKeAPIException("参数为空");
        }
        UserBusinessInfo userBusinessInfo = new UserBusinessInfo();
        userBusinessInfo.setSignatoryName(companyName);
        userBusinessInfo.setSignatoryCreditCode(creditCode);
        userBusinessInfo.setSignatoryLegalPerson(legalPerson);
        List<UserBusinessInfo> businessInfoList = userBusinessInfoMapper.select(userBusinessInfo);
        if(businessInfoList.size() > 0){
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERRCODE_BUSINESSEXIST,"工商信息已存在,请重新上传");
        }
        com.aliyun.ocr_api20210707.Client client = this.createVerifyBusinessLicenseClient();
        String data = "";
        VerifyBusinessLicenseRequest licenseRequest = new VerifyBusinessLicenseRequest()
                .setCreditCode(creditCode)
                .setCompanyName(companyName)
                .setLegalPerson(legalPerson);
        VerifyBusinessLicenseResponse licenseResponse = client.verifyBusinessLicense(licenseRequest);
        data = licenseResponse.getBody().getData();
        VerifyBusinessData verifyBusinessData = JSONObject.parseObject(data, VerifyBusinessData.class);
        return verifyBusinessData;
    }


    public Client createVerifyIdCardClient() throws Exception{
        Config config = new Config()
                .setAccessKeyId("LTAI5tS5hTK2DiiXJJW4kAEB")
                .setAccessKeySecret("jS9YRK4eUyrseNT8crMJt3kT9E0P2t");
        config.endpoint = "dytnsapi.aliyuncs.com";
        return new Client(config);
    }

    public com.aliyun.ocr_api20210707.Client createVerifyBusinessLicenseClient() throws Exception{
        Config config = new Config()
                .setAccessKeyId("LTAI5tS5hTK2DiiXJJW4kAEB")
                .setAccessKeySecret("jS9YRK4eUyrseNT8crMJt3kT9E0P2t");
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        return new com.aliyun.ocr_api20210707.Client(config);
    }


}
